<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Promise Object</h1>
<h2>The then() Method</h2>

<p id="demo"></p>

<script>
function myDisplayer(some) {
  document.getElementById("demo").innerHTML = some;
}

var func=function(myResolve, myReject) {
  let x = 1; //try to change x to 5
 // 生产者部分，some producing code bala bala
 
 //消费者部分，定义成功的条件是x == 0，否则失败
  if (x == 0) {
    myResolve("OK");  //执行resolve
  } else {
    myReject("Error");
  }
}

let myPromise = new Promise(
func
);

var success=  function(value) {myDisplayer(value);}
var failure=function(error) {myDisplayer(error);}

myPromise.then( //可以传入不同的消费者（函数）
  success,
  failure
);

//
</script>

</body>
</html>
